/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myrtxtest;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 *
 * @author ben
 */
public class rtxSender {

    private static String url = "";
    private String ls_rsv = "";
    private String ls_msg = "";

    public void rtxSend(String recv, String msgs) {

        try {
            String ls_rc;
            /**  读取jar包外的配置文件方法，未调通
            FileReader   reader   =   new   FileReader(   "./config.properties"   );
            Properties p = new Properties();
            p.load(reader);
            System.out.println(p.getProperty("url"));
            reader.close();
            */
            InputStream is = this.getClass().getClassLoader().getResourceAsStream("setup.properties");
            Properties pro = new Properties();
            pro.load(is);
            url=pro.getProperty("url");
            is.close();
            HttpClient client = new HttpClient();
            PostMethod method = new PostMethod(url);
            try {
                // Provide custom retry handler is necessary
                method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false));
                // set the charset
                method.getParams().setContentCharset("GBK");
                method.getParams().setHttpElementCharset("GBK");
                ls_rsv = recv;
                ls_msg = msgs;
                NameValuePair rcv = new NameValuePair("receiver", ls_rsv);
                NameValuePair msg = new NameValuePair("msg", ls_msg);
                NameValuePair[] data = {rcv, msg};
                method.setRequestBody(data);
                // Execute the method.
                int statusCode = client.executeMethod(method);
                if (statusCode != HttpStatus.SC_OK) {
                    System.err.println("Method failed: " + method.getStatusLine());
                }
                // Read the response body.
                byte[] responseBody = method.getResponseBody();
                // Deal with the response.
                // Use caution: ensure correct character encoding and is not
                // binary
                // data
                // System.out.println(new String(responseBody));
                ls_rc = new String(responseBody, "GBK");
               // System.out.println(ls_rc);
            } catch (HttpException er) {
                 Logger.getLogger(rtxSender.class.getName()).log(Level.SEVERE, null, er);
            } catch (IOException e) {
                Logger.getLogger(rtxSender.class.getName()).log(Level.SEVERE, null, e);
            } catch (Exception e) {
               Logger.getLogger(rtxSender.class.getName()).log(Level.SEVERE, null, e);
            }
        } catch (IOException ex) {
            Logger.getLogger(rtxSender.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
